<?php

// 员工管理

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
use Session;
use Crypt;

class AdminController extends Controller
{
	
	
	// 获取 员工的 工资日志
	public function getPayLog( Request $request )
	{	
		$data = $request -> except('_token');
		if( !isset($data['admin_id']) )
		{
			exit('缺少员工id');
		}
		$logs = DB::table('admin_log as al')
				// 发放时间 基本工资 补贴 绩效 扣款 实际发放 员工名字
				-> select( 'al.at','al.base','al.subsidy','al.performance','al.cut','al.total','a.name as adminName' )
				-> leftJoin('admin as a','a.id','=','al.admin_id')
				// 员工id
				-> where( 'al.admin_id',$data['admin_id'] )
				-> orderBy('al.at','desc')
				-> get();
		$html = "";
		foreach( $logs as $info )
		{
			$html .= "<div  style='height:70px;width:100%;display:block;float:left;'>";
			$html .= "	<h3 class='popover-title'>".$info->at."</h3>";
			$html .= "	<div class='popover-content'>底薪：".$info->base." &nbsp;补贴：".$info->subsidy." &nbsp;绩效：".$info->performance." &nbsp;扣款：".$info->cut." &nbsp;<span style='color:red;' >实际发放：</span>".$info->total."</div>";
			$html .= "</div>";
		}
		exit($html);
	}
	
	
	// 执行 发工资
	public function payOff( Request $request )
	{
		// 接收数据
		$data = $request -> except('_token');
		if( empty($data['admin_id']) )
		{
			$this -> ajAxReturn('缺少员工id');
		}
		// 获取 员工信息
		$admin_info = DB::table('admin')
				-> where( 'id',$data['admin_id'] )
				-> first();
		if( empty($admin_info) )
		{
			$this -> ajAxReturn('员工id有误');
		}
		if( !isset($data['base']) )
		{
			$this -> ajAxReturn('缺少底薪');
		}
		// 正则验证格式 1到5位 整数
		$pattern = "/^\d{1,5}$/";
		
		// 底薪
		preg_match_all($pattern,$data['base'],$base_arr);
		// 符合格式条件
		if( count($base_arr[0])>0 )
		{
			// 底薪金额 小于0
			if( $base_arr[0][0]<=0 )
			{
				$this -> ajAxReturn('请输入底薪');
			}
		}else
		{
			$this -> ajAxReturn('请输入底薪');
		}
		// 补贴
		if( isset($data['subsidy']) && !empty($data['subsidy']) )
		{
			preg_match_all($pattern,$data['subsidy'],$subsidy_arr);
			// 符合格式条件
			if( count($subsidy_arr[0])>0 )
			{
				// 补贴金额小于0
				if( $subsidy_arr[0][0]<=0 )
				{
					$this -> ajAxReturn('请输入补贴');
				}
			}else
			{
				$this -> ajAxReturn('请输入补贴');
			}
		}else
		{
			$data['subsidy'] = 0;
		}
		// 绩效
		if( isset($data['performance']) && !empty($data['performance']) )
		{
			preg_match_all($pattern,$data['performance'],$performance_arr);
			// 符合格式条件
			if( count($performance_arr[0])>0 )
			{
				// 绩效金额小于0
				if( $performance_arr[0][0]<100 )
				{
					$this -> ajAxReturn('请输入绩效');
				}
			}else
			{
				$this -> ajAxReturn('请输入绩效');
			}
		}else
		{
			$data['performance'] = 0;
		}
		// 扣款
		if( isset($data['cut']) && !empty($data['cut']) )
		{
			preg_match_all($pattern,$data['cut'],$cut_arr);
			// 符合格式条件
			if( count($cut_arr[0])>0 )
			{
				// 扣款金额小于0
				if( $cut_arr[0][0]<10 )
				{
					$this -> ajAxReturn('请输入扣款');
				}
			}else
			{
				$this -> ajAxReturn('请输入扣款');
			}
		}else
		{
			$data['cut'] = 0;
		}
		
		// 发放时间
		$data['at'] = date( 'Y-m-d H:i:s',time() );
		$data['boss'] = Session::get('admin_id');
		$data['total'] = $data['base'] + $data['subsidy'] + $data['performance'] - $data['cut'];
		// 写进日志记录
		$insert_id = DB::table('admin_log')->insertGetId( $data );
		if( $insert_id )
		{
			$this -> ajAxReturn('发放成功');
		}
		$this -> ajAxReturn('发放失败');
	}
	
	
	// 工资列表
    public function boss()
	{
		// 获取路由
		$route_array = request()->route()->getAction();
		$controller = explode("\\",$route_array['controller']);
		$result = explode("@",$controller[3]);
		// 控制器 	$result[0];
		// 方法 	$result[1];
		$auth_final = '';
		foreach( Session::get('auths') as $key=> $auth_info )
		{
			// 符合条件
			if( $auth_info['controller']==$result[0] && $auth_info['func']==$result[1] )
			{
				$auth_final = true;
				break;
			}else
			{
				$auth_final = false;
			}
		} 
		// 权限验证失败
		if( !$auth_final )
		{
			return back()->with( ['authinfo'=>'没有此项权限'] );
		}
		// 查询职工 每页20条数据
		$admin = DB::table('admin as a')
				-> select( 'a.id','a.name','a.lasttime' )
				-> orderBy( 'a.id','desc' ) 
				-> paginate(20);
		$latest = array();	
		foreach( $admin as $key => $admin_info )
		{
			$gongzi = DB::table('admin_log as al')
				// 发放时间 基本工资 补贴 绩效 扣款 实际发放
				-> select( 'al.at','al.base','al.subsidy','al.performance','al.cut','al.total' )
				-> orderBy( 'al.at','desc' ) 
				-> where('al.admin_id',$admin_info->id)
				// 取出最近一次的工资日志
				-> first();
			if( !empty($gongzi) )
			{
				$latest[$admin_info->id]['at'] = $gongzi->at;
				$latest[$admin_info->id]['base'] = $gongzi->base;
				$latest[$admin_info->id]['subsidy'] = $gongzi->subsidy;
				$latest[$admin_info->id]['performance'] = $gongzi->performance;
				$latest[$admin_info->id]['cut'] = $gongzi->cut;
				$latest[$admin_info->id]['total'] = $gongzi->total;
			}else
			{
				$latest[$admin_info->id]['base'] = '';
				$latest[$admin_info->id]['subsidy'] = '';
				$latest[$admin_info->id]['performance'] = '';
				$latest[$admin_info->id]['cut'] = '';
				$latest[$admin_info->id]['at'] = '';
				$latest[$admin_info->id]['total'] = '';
			}
			
		}
		
		return view( 'boss.list',[ 'admin'=>$admin,'latest'=>$latest ] );
	
	}
	
	
	// 执行 修改 添加 权限
	public function addAuth( Request $request )
	{
		// 包含 节点id 和 admin_id
		$data = $request -> except('_token');
		// 清除无效值
		unset($data['all'][0]);
		// 查看该管理员 历史权限
		$history = DB::table('node_admin')
				-> where('admin_id',$data['admin_id'] )
				-> get();
		if( count($history)>0 )
		{
			// 删除 历史权限
			$history = DB::table('node_admin')
				-> where('admin_id',$data['admin_id'] )
				-> delete();
		}
		
		foreach( $data['all'] as $key => $node )
		{
			$insert = array();
			$insert['admin_id'] = $data['admin_id'] ;
			$insert['node_id'] = $node;
			// 赋予 新的权限
			$insert_id = DB::table('node_admin')->insertGetId($insert);
		}
		
		$this->ajAxReturn('权限更改成功');
		
		
	}
	
	
	// 加载 权限列表
	public function authLoad( $admin_id='',Request $request )
	{
		
		if( $admin_id=='' )
		{
			// 先跳到列表页 得有列表的权限 否则死循环 重定向
			return redirect('/admin');
		}
		
		
		// 获取路由
		$route_array = request()->route()->getAction();
		// url地址
		// var_dump( $request->url() );
		$controller = explode("\\",$route_array['controller']);
		$result = explode("@",$controller[3]);
		
		// 控制器 	$result[0];
		// 方法 	$result[1];
		$auth_final = '';
		foreach( Session::get('auths') as $key=> $auth_info )
		{
			// 符合条件
			if( $auth_info['controller']==$result[0] && $auth_info['func']==$result[1] )
			{
				$auth_final = true;
				break;
			}else
			{
				$auth_final = false;
			}
		} 
		// 权限验证失败
		if( !$auth_final )
		{
			return back()->with( ['authinfo'=>'没有此项权限'] );
		}
		
		// 获取 去重后的 控制器 
		$controllers = DB::table('node')
				-> select( DB::raw("distinct(controller)"),'desc' )
				-> get();
		
		$functions = array();
		foreach( $controllers as $key=> $controller )
		{
			// 该控制器下的方法
			$funs = DB::table('node')
				-> where('controller',$controller->controller)
				-> get();
			foreach( $funs as $k=> $funcinfo )
			{
				$fun = array();
				$fun['id'] = $funcinfo->id;
				$fun['name'] = $funcinfo->name;
				$fun['controller'] = $funcinfo->controller;
				$fun['func'] = $funcinfo->func;
				// 获取 全部方法
				$functions[$key][$k] = $fun;
			}
		
		}
		
		// 历史权限
		$auth_history = DB::table('node_admin')
			-> where('admin_id',$admin_id)
			-> get();
		$auth = array();
		foreach( $auth_history as $key => $auth_info )
		{
			// 节点id
			$auth[] = $auth_info -> node_id;
		}
		
		
		$admin = DB::table('admin')
			-> where('id',$admin_id)
			-> first();
		
		
		// 权限列表
		return view('admin.auth',['admin'=>$admin,'controllers'=>$controllers,'functions'=>$functions,'auth'=>$auth]);
	
	}
	
	
	// 执行 职工 添加 编辑
    public function doAdd( Request $request )
    {
		$data = $request -> except('_token');
		
		// 名字 
		$name_length=mb_strlen( $data['name'],'utf-8' );
		if( $name_length<1 )
		{
			$this->ajAxReturn('职工名字不能为空');
		}
		
		// 名字已存在
		$admin_history = DB::table('admin')
				-> where('name', $data['name'])
				-> first();
		if( !empty($admin_history) )
		{
			$this->ajAxReturn('名字已存在');
		}
		
		
		if( empty($data['pass']) )
		{
			$this->ajAxReturn( '请输入登录密码' );
		}
		
		if( empty($data['repass']) )
		{
			$this->ajAxReturn( '请输入确认密码' );
		}
		
		
		if( $data['pass']!=$data['repass'] )
		{
			$this->ajAxReturn( '两次密码输入不一致' );
		}
		$admin_id = $data['admin_id'];
		unset($data['repass'],$data['admin_id']);
		
		if( $admin_id == '' )
		{
			
			$data['regtime']=date('Y-m-d H:i:s',time());
			$data['lasttime']=date('Y-m-d H:i:s',time());
			// 创建
			$insert_id = DB::table('admin')->insertGetId($data);
			if( $insert_id )
			{
				$this->ajAxReturn( '职工创建成功' );
			}else
			{
				$this->ajAxReturn( '操作失败' );
			}
		}else
		{
			// 编辑
			$update = DB::table('admin')
					-> where('id',$admin_id )
					-> update( $data );
			if( $update )
			{
				$this -> ajAxReturn( '职工编辑成功' );
			}else
			{
				$this -> ajAxReturn( '操作失败' );
			}
			
		}
		

    }
	
	
	// 职工 添加 编辑 页面
    public function add( $admin_id='' )
    {
		// 获取路由
		$route_array = request()->route()->getAction();
		$controller = explode("\\",$route_array['controller']);
		$result = explode("@",$controller[3]);
		// 控制器 	$result[0];
		// 方法 	$result[1];
		$auth_final = '';
		foreach( Session::get('auths') as $key=> $auth_info )
		{
			// 符合条件
			if( $auth_info['controller']==$result[0] && $auth_info['func']==$result[1] )
			{
				$auth_final = true;
				break;
			}else
			{
				$auth_final = false;
			}
		} 
		// 权限验证失败
		if( !$auth_final )
		{
			return back()->with( ['authinfo'=>'没有此项权限'] );
		}
		
		
		if( $admin_id=='' )
		{
			// 添加
			return view( 'admin.add' );
		}else
		{
			// 编辑
			$admin_info = DB::table('admin')
					-> where('id', $admin_id)
					-> first();
			return view( 'admin.add',[ 'admin_info'=>$admin_info ] );
		}
		
    }
	
	
	// 职工 列表
    public function index( Request $request )
    {
		// 获取路由
		$route_array = request()->route()->getAction();
		$controller = explode("\\",$route_array['controller']);
		$result = explode("@",$controller[3]);
		// 控制器 	$result[0];
		// 方法 	$result[1];
		$auth_final = '';
		foreach( Session::get('auths') as $key=> $auth_info )
		{
			// 符合条件
			if( $auth_info['controller']==$result[0] && $auth_info['func']==$result[1] )
			{
				$auth_final = true;
				break;
			}else
			{
				$auth_final = false;
			}
		} 
		// 权限验证失败
		if( !$auth_final )
		{
			return back()->with( ['authinfo'=>'没有此项权限'] );
		}
		
		
		// 获取 搜索 关键词
		$keyword = $request->input('keyword',"");
		if( empty($keyword) )
		{
			// 获取全部
			$one = 'a.id';
			$two = '!=';
			$three = '0';
		}else
		{
			// 职工账户
			$one = 'a.name';
			$two = 'like';
			$three = '%'.$keyword.'%';
		}
		
		// 查询职工 每页20条数据
		$admin = DB::table('admin as a')
				-> select( 'a.id','a.name','a.lasttime' )
				-> where( $one,$two,$three ) 
				-> orderBy( 'a.id','desc' ) 
				-> paginate(20);
		return view( 'admin.list',[ 'admin'=>$admin,'request'=>$keyword ] );
    }
	
	


}
